﻿using ImoviesBlazor.Server.Data;
using ImoviesBlazor.Shared.Entities;

namespace ImoviesBlazor.Server.Repository
{
    public class TeleplayRepository : ITeleplayRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public TeleplayRepository(ApplicationDbContext dbContext)
        {
            this._dbContext = dbContext;
        }


        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<Teleplay>> GetAllListAsync()
        {
            return Task.FromResult(_dbContext.Set<Teleplay>().OrderByDescending(x => x.Year).AsEnumerable());
        }




        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public Task<IEnumerable<Teleplay>> GetListAsync(int pageSize, int pageNum)
        {
            return Task.FromResult(_dbContext.Set<Teleplay>()
                .OrderByDescending(x => x.Year)
                .Skip((pageNum - 1) * pageSize)
                .Take(pageSize)
                .AsEnumerable());
        }


        /// <summary>
        /// 获取视频播放链接列表
        /// </summary>
        /// <param name="vid"></param>
        /// <returns></returns>
        public Task<IEnumerable<TeleplayVlink>> GetVlinkList(string vid)
        {
            return Task.FromResult(_dbContext.Set<TeleplayVlink>()
            .Where(x => x.TeleplayId == vid)
            .AsEnumerable());
        }


        /// <summary>
        /// 查询所有年份
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<uint>> GetAllYearsAsync()
        {
            return Task.FromResult(_dbContext.Set<Teleplay>()
                .Select(x => x.Year)
                .AsEnumerable());
        }


        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <returns></returns>
        public Task<int> GetTotal()
        {
            return Task.FromResult(_dbContext.Set<Teleplay>().Count());
        }

        /// <summary>
        /// 添加电视剧
        /// </summary>
        /// <param name="teleplay"></param>
        public void AddTeleplay(Teleplay teleplay)
        {
            _dbContext.Set<Teleplay>().Add(teleplay);
        }


        /// <summary>
        /// 批量添加电视剧
        /// </summary>
        /// <param name="teleplayList"></param>
        /// <returns></returns>
        public async Task BatchAddTeleplay(List<Teleplay> teleplayList)
        {
            await _dbContext.Set<Teleplay>().AddRangeAsync(teleplayList);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="teleplay"></param>
        public void UpdateTeleplay(Teleplay teleplay)
        {
            _dbContext.Set<Teleplay>().Update(teleplay);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="teleplayId"></param>
        public async Task DeleteTeleplay(string teleplayId)
        {
            var t = await _dbContext.Set<Teleplay>().FindAsync(teleplayId);
            if (t != null)
            {
                _dbContext.Set<Teleplay>().Remove(t);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public async Task<bool> SaveAsync()
        {
            return await _dbContext.SaveChangesAsync() > 0;
        }


    }
}
